<?php

namespace app\api\controller;

use app\api\model\Coupon AS CouponModel;
use app\api\validate\Page;

class Coupon extends Base
{
    /**
     * 优惠券列表
     * @return array
     * @throws \think\Exception\DbException
     */
    public function index()
    {
        // 使用情况
        $type = input('type');
        $page = $data['page'] = input('page') ? input('page') : 1;
        $size = $data['size'] = input('size') ? input('size') : $this->pageSize;
        if (empty($type)) {
            return self::sendResult(100);
        }

        $typeArr = CouponModel::attributes('type');
        if (!isset($typeArr[$type])) {
            return self::sendResult(102, [], '类型错误');
        }

        // 检测变量
        $validate = new Page();
        if (!$validate->check($data)) {
            return self::sendResult(102, [], $validate->getError());
        }

        $oauthId = $this->oauthInfo['id'];
        $model = CouponModel::getList($oauthId, $type, $page, $size);

        return self::sendResult(self::$okCode, $model);
    }
}